import { delay, ensurePageOpen } from '../utils.js';
import { NotificationService } from '../services/notification.js';
import { notificationConfig } from '../config/index.js';
export class BaseStrategy {
  constructor(context, config) {
    this.context = context;
    this.config = config;
    this.baseUrl = config.baseUrl;
    // 初始化通知服务
    this.notificationService = new NotificationService({
      dingtalk: notificationConfig?.dingtalk
    });
  }

  // 已签收未命中签收规则，发送通知
  async sendNotification(shopName, orderId, refundId) {
    logInfo(`[BaseStrategy] ${shopName} 订单 ${orderId} 已签收但未命中签收规则，发送通知`);

    try {
      await this.notificationService.sendDingTalkMessage({
        customOptions: {
          msgtype: 'markdown',
          markdown: {
            title: `${shopName} 退款异常通知`,
            text: `### ${shopName} 退款异常通知

                    订单编号: ${orderId}

                    退款编号: ${refundId}

                    状态: 已签收但未命中签收规则

                    请及时处理！`
          }
        }
      });
      logInfo(`[BaseStrategy] ${shopName} 退款异常通知发送成功`);
    } catch (error) {
      logError(`[BaseStrategy] ${shopName} 退款异常通知发送失败: ${error.message}`);
    }
  }
  pageLoad(page) {
    page.on('load', async () => {
      if (!this.config.useCloudCookie) {
        if (this.config.storageState) {
          await this.context.storageState({ path: this.config.storageState });
          if (typeof logInfo === 'function') {
            logInfo(`[BaseStrategy] storageState 已保存到 ${this.config.storageState}`);
          }
        }
      } else {
        if (typeof logInfo === 'function') {
          logInfo('[BaseStrategy] 已启用 cloud_cookie，跳过 storageState 保存');
        }
      }
    });
  }
}
